/*
* EnthalpyController.java
* Created on  2015/6/2 19:14
* 版本       修改时间          作者      修改内容
* V1.0.1    2015/6/2       xfz    初始版本
*
*/

package com.infitecs.eyas.enthalpy;

import com.infitecs.eyas.enthalpy.model.Enthalpy;
import com.infitecs.eyas.enthalpy.service.EnthalpyService;
import fr.expression4j.core.exception.EvalException;
import fr.expression4j.core.exception.ParsingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 类的描述信息
 *
 * @author xfz
 * @version 1.0.1
 */
@Controller
@RequestMapping("/enthalpy/")
public class EnthalpyController {

    @Autowired
    private EnthalpyService enthalpyService;

    @RequestMapping(value = "enthalpy")
    public String getEnthalpy(HttpSession session){
        try {
            int caseId = Integer.parseInt(String.valueOf(session.getAttribute("caseId")));
            Enthalpy enthalpy = enthalpyService.getEnthalpy(caseId);
            if (enthalpy == null) {
                enthalpyService.insertCaseId(caseId);
            }
        } catch (Exception e) {
            return "enthalpy/enthalpy";
        }
        return "enthalpy/enthalpy";
    }

    @RequestMapping(value = "saveB2", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Double> saveB2(@RequestParam("b2") double b2, HttpSession session) throws ParsingException, EvalException {
        int caseId = Integer.parseInt(String.valueOf(session.getAttribute("caseId")));
        enthalpyService.saveB2(b2,caseId);
        return enthalpyService.culH2(b2,caseId);
    }

    @RequestMapping(value = "saveC2", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Double> saveC2(@RequestParam("c2") double c2,HttpSession session) throws ParsingException, EvalException {
        int caseId = Integer.parseInt(String.valueOf(session.getAttribute("caseId")));
        enthalpyService.saveC2(c2,caseId);
        return enthalpyService.culAndSaveD2(c2,caseId);
    }

    @RequestMapping(value = "culF2AndI2", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Double> culF2AndI2(HttpSession session) throws ParsingException, EvalException {
        int caseId = Integer.parseInt(String.valueOf(session.getAttribute("caseId")));
        return enthalpyService.culF2AndI2(caseId);
    }

    @RequestMapping(value = "culB6", method = RequestMethod.GET)
    @ResponseBody
    public double culB6(HttpSession session) throws ParsingException, EvalException {
        int caseId = Integer.parseInt(String.valueOf(session.getAttribute("caseId")));
        return enthalpyService.culB6(caseId);
    }

    @RequestMapping(value = "recover", method = RequestMethod.GET)
    @ResponseBody
    public Enthalpy getEnthalpyRecover(HttpSession session) {
        int caseId = Integer.parseInt(String.valueOf(session.getAttribute("caseId")));
        return enthalpyService.getEnthalpy(caseId);
    }
}
